package com.chixing.ih.module.programmer.service.Impl;

import com.chixing.ih.module.programmer.mapper.ImageMapper;
import com.chixing.ih.module.programmer.mapper.PersonalProjectMapper;
import com.chixing.ih.module.programmer.pojo.PersonalProject;
import com.chixing.ih.module.programmer.pojo.ProjectImage;
import com.chixing.ih.module.programmer.pojo.dto.PersonalProjectDTO;
import com.chixing.ih.module.programmer.service.IPersonalProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/5
 * Time: 17:31
 */
@Service
@Transactional
public class PersonalProjectServiceImpl implements IPersonalProjectService {

    @Autowired
    PersonalProjectMapper personalProjectMapper;

    @Autowired
    ImageMapper imageMapper;

    @Override
    public boolean updateList(List<PersonalProject> personalProjects) {
        for (PersonalProject personalProject : personalProjects
        ) {
            int record = personalProjectMapper.updateOne(personalProject);
            if (record == 0)
                return false;
        }
        return true;
    }

    @Override
    public List<PersonalProjectDTO> listAll(long userId) {
        List<PersonalProjectDTO> personalProjectDTOS = personalProjectMapper.listAll(userId);
        if (personalProjectDTOS == null)
            return null;
        for (PersonalProjectDTO personalProjectDTO : personalProjectDTOS
        ) {
            ProjectImage oneByProjectId = imageMapper.getOneByProjectId(personalProjectDTO.getPersonalProject().getId());
            if (oneByProjectId != null)
                personalProjectDTO.setImageUrl(oneByProjectId.getImageUrl());

        }
        return personalProjectDTOS;


    }

    @Override
    public PersonalProject savePersonalProject(PersonalProject personalProject) {

        int record = personalProjectMapper.insertOne(personalProject);
//            ProjectImage projectImage = new ProjectImage();
//            projectImage.setProjectId(personalProject.getId());
//            int record2 = imageMapper.insertProjectOne(projectImage);
        if (record > 0)
            return personalProject;
        return null;



    }

    @Override
    public boolean deletePersonalProject(long id) {
        int record = personalProjectMapper.deleteOne(id);
        if (record <= 0)
            return false;
        return true;


    }
}
